<template>
  <div>
    <el-card>
      <template #header>
        <div class="add">
          <h4>商品分类</h4>
          <el-button type="primary" size="mini" @click="dialogVisible = true"
            >添加分类</el-button
          >
        </div>
      </template>
      <div>
        <el-table
          ref="singleTable"
          :data="tableData"
          highlight-current-row
          @current-change="handleCurrentChange1"
          style="width: 100%"
        >
          <el-table-column property="id" label="序号"> </el-table-column>
          <el-table-column label="分类名称">
            <template v-slot="{ row }">
              <span v-if="row.flag">{{ row.cateName }}</span>
              <el-input v-else v-model="row.cateName" size="mini"></el-input>
            </template>
          </el-table-column>
          <el-table-column property="state" label="是否启用">
            <template v-slot="{ row }">
              <el-switch
                :disabled="row.flag"
                v-model="row.state"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="0"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column property="address" label="操作">
            <template v-slot="{ row }">
              <el-button
                :type="row.flag ? '' : 'primary'"
                size="mini"
                @click="edit(row)"
                >{{ row.flag ? '编辑' : '保存' }}</el-button
              >
              <el-button size="mini" type="danger" @click="ifydel(row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="5"
          layout="total, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>

        <!-- 添加分类 -->
        <el-dialog
          title="添加分类"
          :visible.sync="dialogVisible"
          width="30%"
          :before-close="handleClose"
        >
          <el-form ref="form" label-width="80px">
            <el-form-item label="分类名称">
              <el-input v-model="name"></el-input>
            </el-form-item>
            <el-form-item label="是否启用">
              <el-switch
                v-model="value"
                active-color="#13ce66"
                inactive-color="#ff4949"
              >
              </el-switch>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addCate">确 定</el-button>
          </span>
        </el-dialog>
      </div>
    </el-card>
  </div>
</template>

<script>
import { catelist, delcate, addcate, editcate } from '../../api/info.js'
export default {
  data() {
    return {
      tableData: [],
      currentRow: null,
      currentPage: 1,
      total: 0,
      dialogVisible: false,
      value: true,
      name: ''
    }
  },
  created() {
    this.getify()
  },
  methods: {
    // 获取分类列表
    async getify() {
      const { data, total } = await catelist({
        currentPage: this.currentPage,
        pageSize: 5
      })
      if (data.length === 0) {
        location.reload()
        console.log('删没了,重新刷新一下')
      }
      console.log(data)
      data.forEach(item => {
        item.flag = true
      })
      this.tableData = data
      this.total = total
    },

    // 编辑
    async edit(row) {
      console.log(row)
      if (row.flag) {
        row.flag = !row.flag
        // console.log('点了编辑')
      } else {
        const { code, msg } = await editcate({
          id: row.id,
          cateName: row.cateName,
          state: Boolean(row.state)
        })
        if (code === 0) {
          this.getify()
        } else {
          this.$message.error(msg)
        }
        row.flag = !row.flag
      }
    },

    // 删除分类
    ifydel(id) {
      this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { code } = await delcate({ id })
          if (code === 0) {
            this.getify()
          } else {
            this.$message.error('删除失败')
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    // 添加分类
    async addCate() {
      const { code } = await addcate({
        cateName: this.name,
        state: this.value
      })
      if (code === 0) {
        this.getify()
      } else {
        this.$message.error('添加失败')
      }
      this.dialogVisible = false
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },

    handleCurrentChange1(val) {
      console.log(val)
      this.currentRow = val
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.currentPage = val
      this.getify()
    }
  }
}
</script>

<style lang="less" scoped>
.el-pagination {
  margin-top: 20px;
}
.add {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
// /deep/.cell {
//   height: 28px;
//   /deep/.el-input {
//     height: 100%;
//     /deep/.el-input__inner {
//       height: 100%;
//     }
//   }
// }
</style>
